#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

const char* ssid = "RedmiK30";
const char* password = "q1w2e3r4";

const char* host = "192.168.0.24"; //需要访问的域名
const int port = 8080;  // 需要访问的端口
const String url = "/computer-manager/manager/echo";   // 需要访问的地址

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password); // 连接WIFI
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); // 显示WIFI地址

}

void loop() {
  Serial.print("connecting to ");
  Serial.println(host);

  WiFiClient client;

  /**
     测试是否正常连接
  */
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  delay(10);

  String postRequest = (String)("POST ") + url + "?msg=test HTTP/1.1\r\n" +
                       "Content-Type: application/json;charset=utf-8\r\n" +
                       "Host: " + host + "\r\n" +
                       "Connection: close\r\n\r\n";
  Serial.println(postRequest);
  client.print(postRequest);  // 发送HTTP请求

  /**
     展示返回的所有信息
  */
  String line = client.readStringUntil('\n');
  Serial.print("status_response: ");
  Serial.println(line);
  // 使用find跳过HTTP响应头
  if (client.find("\r\n\r\n")) {
    Serial.println("Found Header End. Start Parsing.");
  }

  String result = client.readStringUntil('\n');
  Serial.print("result = ");
  Serial.println(result);

  client.stop();
  delay(3000);
}
